Import Libraries¶
In [1]:
import os
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from DQN_Binary.utils import *
import plotly.graph_objs as go
import plotly.subplots as sp
sns.set_theme()
In [2]:
og_path = os.getcwd()
og_path
Out[2]:
'/Users/Roy/Desktop/College/GeorgiaTech/Research/Portfolio-Rebalancing-DQN'
Initial¶
In [3]:
n_asset = 2
In [4]:
mu = np.linspace(50, 200, n_asset) / 1e4
sigma = np.linspace(300, 800, n_asset) / 1e4
cov = np.diag(sigma ** 2)
In [5]:
tc_list = [0, 0.0005, 0.001, 0.002, 0.005, 0.01, 0.05]
In [6]:
x = np.arange(1, 101)
state_possible = np.array(np.meshgrid(*([x] * n_asset))).T.reshape(-1, n_asset).astype(np.float32)
state_possible = state_possible[state_possible.sum(axis=1) == 100, :] / 100
DP¶
In [7]:
dp_path = og_path + '/DP_Binary/results/models/'
os.chdir(dp_path)
In [8]:
df_0 = pd.read_csv("dp_tc_0_assets_2", index_col=0, dtype=np.float32)
df_01 = pd.read_csv("dp_tc_0.001_assets_2", index_col=0, dtype=np.float32)
df_02 = pd.read_csv("dp_tc_0.002_assets_2", index_col=0, dtype=np.float32)
df_05 = pd.read_csv("dp_tc_0.0005_assets_2", index_col=0, dtype=np.float32)
df_5 = pd.read_csv("dp_tc_0.005_assets_2", index_col=0, dtype=np.float32)
df_1 = pd.read_csv("dp_tc_0.01_assets_2", index_col=0, dtype=np.float32)
df_5 = pd.read_csv("dp_tc_0.05_assets_2", index_col=0, dtype=np.float32)
In [9]:
df_list = [df_0, df_05, df_01, df_02, df_5, df_1, df_5]
In [10]:
x = pd.to_numeric(df_0.index.values)
action_possible = pd.to_numeric(df_0.columns.values)
In [11]:
action_df = pd.DataFrame(index=x)
action_bm_df = pd.DataFrame(index=x)
for tc, bell in zip(tc_list, df_list):
# visualize q table
action = np.array([action_possible[i] for i in np.argmax(bell, axis=1)])
action_df[f"TC: {tc * 1e4:.0f} bps BM"] = action
action_bm = []
for i in x:
a = x[np.argmax([net_sharpe(np.array([j, 1 - j]), mu, cov, np.array([i, 1 - i]), tc) for j in x])] - i
action_bm.append(a)
action_bm = np.array(action_bm)
action_bm_df[f"TC: {tc * 1e4:.0f} bps BM (GT)"] = action_bm
In [12]:
optimal_weight = find_optimal_wgt(mu, cov)
optimal_weight
Out[12]:
array([0.63941273, 0.36058727])
In [13]:
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
action_df.plot(ax=ax, color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728'])
action_bm_df.plot(ax=ax, color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728'], linestyle="dashed", alpha=0.5)
ax.set_xlabel("Weight on First Asset")
ax.set_ylabel("Suggested Delta Weight on Asset 1")
ax.axvline(optimal_weight[0], color="red", linestyle="dotted")
ax.axhline(0, color="red", linestyle="dotted")
ax.legend()
plt.tight_layout()
plt.show()
plt.close()
In [14]:
# Create subplots
fig = sp.make_subplots(rows=1, cols=1, subplot_titles="Comparison of BM and Net Sharpe State Possible Outputs", shared_yaxes=True)
# Add traces for Action DQN and Action BM
for col, color, linestyle in zip(action_df.columns, ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#2a8bde', '#ff6e51', '#4caf50'], ['solid']*len(action_df.columns)):
trace_dqn = go.Scatter(x=action_df.index, y=action_df[col], mode='lines', name=f'Action BM - {col}', line=dict(color=color, dash="solid"))
fig.add_trace(trace_dqn, row=1, col=1)
# Add traces for Action DQN and Action BM
for col, color, linestyle in zip(action_bm_df.columns, ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#2a8bde', '#ff6e51', '#4caf50'], ['dot']*len(action_bm_df.columns)):
trace_bm = go.Scatter(x=action_bm_df.index, y=action_bm_df[col], mode='lines', name=f'Ground Truth - {col}', line=dict(color=color, dash=linestyle))
fig.add_trace(trace_bm, row=1, col=1)
# Add vertical and horizontal lines
fig.add_shape(dict(
type="line",
x0=optimal_weight[0],
x1=optimal_weight[0],
y0=action_df.values.min(),
y1=action_df.values.max(),
line=dict(color="gold", dash="dot")
))
fig.add_shape(dict(
type="line",
x0=action_df.index.min(),
x1=action_df.index.max(),
y0=0,
y1=0,
line=dict(color="gold", dash="dot")
))
# Update layout
fig.update_layout(
title="Comparison of BM and Net Sharpe State Possible Outputs",
xaxis=dict(title="Weight on First Asset"),
yaxis=dict(title="Suggested Delta Weight on Asset 1"),
showlegend=True,
)
# Show the figure
fig.show()
- Change DP ground truth to run on binary
- DQN still weird need debug with higher TC
- Decision boundary changed so need to re-run DP
DQN Binary¶
In [15]:
dqn_binary = og_path + '/DQN_Binary/'
os.chdir(dqn_binary)
In [16]:
# tmp=pd.read_csv("states_2.csv", on_bad_lines='skip')
# tmp.value_counts(normalize=True).plot(kind='bar', figsize=(16, 9), )
Out[16]:
<Axes: xlabel='0.48,0.52'>
RL:¶
In [16]:
# RL:
BATCH_SIZE = 512
GAMMA = 0.7
EPS_START = 0.999
EPS_END = 0.05
EPS_DECAY = 10000
TAU = 0.005
LR = 1e-6
mem_cap = 100000
num_episodes = 10000000
In [20]:
dqn_bool_action_df = pd.DataFrame(index=x)
In [21]:
from DQN_Binary.DQN_Binary import *
In [140]:
for tc in tc_list:
agent = Agent(mu=mu, cov=cov, batch_size=BATCH_SIZE, gamma=GAMMA,
eps_start=EPS_START, eps_end=EPS_END, eps_decay=EPS_DECAY, tau=TAU, lr=LR,
tc=tc)
agent.load_models()
action = []
for current_state in state_possible:
current_state = torch.Tensor(current_state)
try:
tmp_action = agent.policy_net(current_state).argmax()
except:
tmp_action = agent.policy_net(current_state[np.newaxis]).argmax()
action.append(tmp_action)
action = np.array([i.item() for i in action])
dqn_bool_action_df[f"TC: {tc * 1e4:.0f} bps DQN"] = action
Saving states to csv... ... loading checkpoint ... ... loading checkpoint ... Saving states to csv... ... loading checkpoint ... ... loading checkpoint ... Saving states to csv... ... loading checkpoint ... ... loading checkpoint ... Saving states to csv... ... loading checkpoint ... ... loading checkpoint ... Saving states to csv... ... loading checkpoint ... ... loading checkpoint ... Saving states to csv... ... loading checkpoint ... ... loading checkpoint ... Saving states to csv... ... loading checkpoint ... ... loading checkpoint ...
In [141]:
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
dqn_bool_action_df.plot(ax=ax, color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728'])
action_df.plot(ax=ax, color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728'], linestyle="dashed")
action_bm_df.plot(ax=ax, color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728'], linestyle="dashed", alpha=0.5)
ax.set_xlabel("Weight on First Asset")
ax.set_ylabel("Suggested Delta Weight on Asset 1")
ax.axvline(optimal_weight[0], color="gold", linestyle="dotted")
ax.axhline(0, color="gold", linestyle="dotted")
ax.legend()
plt.tight_layout()
plt.show()
plt.close()
In [142]:
# Assuming `dqn_bool_action_df`, `action_df`, and `optimal_weight` are available
# Create subplots
fig = sp.make_subplots(rows=1, cols=1, subplot_titles=("Comparison of DQN and Action"), shared_yaxes=True)
# Add traces for DQN and Action
for col, color, linestyle in zip(dqn_bool_action_df.columns, ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#2a8bde', '#ff6e51', '#4caf50'], ['solid']*len(dqn_bool_action_df.columns)):
trace_dqn = go.Scatter(x=dqn_bool_action_df.index, y=dqn_bool_action_df[col], mode='lines', name=f'DQN - {col}', line=dict(color=color, dash=linestyle))
fig.add_trace(trace_dqn, row=1, col=1)
# Add traces for DQN and Action
for col, color, linestyle in zip(action_df.columns, ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#2a8bde', '#ff6e51', '#4caf50'], ['dash']*len(action_df.columns)):
trace_action = go.Scatter(x=action_df.index, y=action_df[col], mode='lines', name=f'BM - {col}', line=dict(color=color, dash=linestyle))
fig.add_trace(trace_action, row=1, col=1)
# Add traces for Action DQN and Action BM
for col, color, linestyle in zip(action_bm_df.columns, ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#2a8bde', '#ff6e51', '#4caf50'], ['dot']*len(action_bm_df.columns)):
trace_bm = go.Scatter(x=action_bm_df.index, y=action_bm_df[col], mode='lines', name=f'Ground Truth - {col}', line=dict(color=color, dash=linestyle))
fig.add_trace(trace_bm, row=1, col=1)
# Add vertical and horizontal lines
fig.add_shape(dict(
type="line",
x0=optimal_weight[0],
x1=optimal_weight[0],
y0=action_df.values.min(),
y1=action_df.values.max(),
line=dict(color="gold", dash="dot")
))
fig.add_shape(dict(
type="line",
x0=dqn_bool_action_df.index.min(),
x1=dqn_bool_action_df.index.max(),
y0=0,
y1=0,
line=dict(color="gold", dash="dot")
))
# Update layout
fig.update_layout(
title="Comparison of DQN and Action",
xaxis=dict(title="Weight on First Asset"),
yaxis=dict(title="Suggested Delta Weight on Asset 1"),
showlegend=True,
)
# Show the figure
fig.show()
In [143]:
# Assuming `dqn_bool_action_df` and `bool_action_bm_df` are your DataFrames
dqn_df = dqn_bool_action_df.copy()
bm_df = action_df.copy()
# Create a heatmap trace for DQN Binary
trace_dqn = go.Heatmap(z=dqn_df, x=dqn_df.columns, y=dqn_df.index, name="DQN Binary")
# Create a heatmap trace for DP Bellman
trace_dp = go.Heatmap(z=bm_df, x=bm_df.columns, y=bm_df.index, name="DP Bellman")
# Create the layout with 'overlay' parameter set to "overlay"
layout = go.Layout(
title="Comparison of DQN Binary and DP Bellman",
xaxis=dict(title="Transaction Cost (bps)",
showgrid=True),
yaxis=dict(title="Weight on First Asset",
showgrid=True),
# barmode="overlay" # Set the barmode to overlay
)
# Add horizontal line at y=0.50
layout.shapes = [
dict(
type="line",
x0=dqn_df.columns[0], # Adjust x0 to the starting point of your data
x1=dqn_df.columns[-1], # Adjust x1 to the ending point of your data
y0=optimal_weight[0],
y1=optimal_weight[0],
line=dict(color="red", width=2), # You can customize the color and width of the line
),
dict(
type="line",
x0=bm_df.columns[0], # Adjust x0 to the starting point of your data
x1=bm_df.columns[-1], # Adjust x1 to the ending point of your data
y0=optimal_weight[0],
y1=optimal_weight[0],
line=dict(color="red", width=2), # You can customize the color and width of the line
)
]
# Create the figure
fig = go.Figure(data=[trace_dp], layout=layout)
fig.show()
fig = go.Figure(data=[trace_dqn], layout=layout)
# Show the figure
fig.show()
# , colorscale="Viridis"
In [134]:
dqn_bool_action_df
Out[134]:
| TC: 0 bps DQN | TC: 5 bps DQN | TC: 10 bps DQN | TC: 20 bps DQN | TC: 50 bps DQN | TC: 100 bps DQN | TC: 500 bps DQN | |
|---|---|---|---|---|---|---|---|
| 0.01 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0.02 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0.03 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0.04 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0.05 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 0.95 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0.96 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0.97 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0.98 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0.99 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
99 rows × 7 columns
In [135]:
dqn_bool_action_df.describe()
Out[135]:
| TC: 0 bps DQN | TC: 5 bps DQN | TC: 10 bps DQN | TC: 20 bps DQN | TC: 50 bps DQN | TC: 100 bps DQN | TC: 500 bps DQN | |
|---|---|---|---|---|---|---|---|
| count | 99.000000 | 99.000000 | 99.0 | 99.000000 | 99.000000 | 99.000000 | 99.000000 |
| mean | 0.151515 | 0.111111 | 0.0 | 0.080808 | 0.050505 | 0.121212 | 0.010101 |
| std | 0.360375 | 0.315869 | 0.0 | 0.273927 | 0.220099 | 0.328035 | 0.100504 |
| min | 0.000000 | 0.000000 | 0.0 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| 25% | 0.000000 | 0.000000 | 0.0 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| 50% | 0.000000 | 0.000000 | 0.0 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| 75% | 0.000000 | 0.000000 | 0.0 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| max | 1.000000 | 1.000000 | 0.0 | 1.000000 | 1.000000 | 1.000000 | 1.000000 |
In [144]:
sns.heatmap(dqn_bool_action_df, cmap='cividis')
y = optimal_weight[0]
ax = plt.gca()
plt.hlines(y=y*100, xmin=0, xmax=len(dqn_bool_action_df.columns), colors='red')
plt.title("DQN Binary Heatmap of Rebalancing")
plt.show()
In [113]:
sns.heatmap(action_df, cmap='cividis')
y = optimal_weight[0]
ax = plt.gca()
plt.hlines(y=y*100, xmin=0, xmax=len(action_df.columns), colors='red')
plt.title("DP Bellman Heatmap of Rebalancing")
plt.show()
In [30]:
# import matplotlib.pyplot as plt
# import seaborn as sns
# import numpy as np
# from matplotlib.patches import Patch
# from matplotlib.lines import Line2D
#
# # Assuming `dqn_bool_action_df` and `bool_action_bm_df` are your DataFrames
# dqn_df = dqn_bool_action_df.copy()
# bm_df = bool_action_bm_df.copy()
#
# # Function to find transitions
# def find_transitions(series):
# transitions = np.where(series.diff().dropna() != 0)[0]
# return transitions
#
# # Plotting
# fig, ax = plt.subplots(figsize=(12, 6))
#
# # Plotting the binary outputs
# sns.heatmap(dqn_df.T, cmap='cividis', ax=ax, cbar=False, linewidths=0.5, linecolor='black', alpha=0.5)
# sns.heatmap(bm_df.T, cmap='cividis', ax=ax, cbar=False, linewidths=0.5, linecolor='black', alpha=0.5)
#
# # Finding transitions for all transaction costs
# for col1, col2 in zip(dqn_df.columns, bm_df.columns):
# dqn_transitions = find_transitions(dqn_df[col1])
# bm_transitions = find_transitions(bm_df[col2])
#
# # Plotting vertical lines for transitions
# for transition in dqn_transitions:
# ax.axvline(x=transition, color='red', linestyle='--', linewidth=1)
# ax.text(transition + 0.5, dqn_df.index[-1] + 0.5, f"{col}: {transition}", ha='center', va='center', color='red', fontsize=8)
#
# for transition in bm_transitions:
# ax.axvline(x=transition, color='blue', linestyle='--', linewidth=1)
# ax.text(transition + 0.5, bm_df.index[-1] + 0.5, f"{col}: {transition}", ha='center', va='center', color='blue', fontsize=8)
#
# # Annotate data point values
# for i in range(len(dqn_df.columns)):
# for j in range(len(dqn_df)):
# ax.text(i + 0.5, j + 0.5, int(dqn_df.iloc[j, i]), ha='center', va='center', color='white', fontsize=8)
#
# # Create proxy artists for legend
# legend_elements = [
# Line2D([0], [0], color='red', linestyle='--', label='DQN Transition'),
# Line2D([0], [0], color='blue', linestyle='--', label='DP Transition'),
# Patch(facecolor='white', edgecolor='black', label='DQN Binary Output'),
# ]
#
# # Set labels and title
# ax.set_xlabel("Observations")
# ax.set_ylabel("Transaction Costs")
# ax.set_title("Comparison of Binary Outputs with Transitions")
#
# # Add legend with custom elements
# ax.legend(handles=legend_elements)
#
# plt.show()
In [31]:
# import matplotlib.pyplot as plt
# import seaborn as sns
# import numpy as np
# from matplotlib.patches import Patch
# from matplotlib.lines import Line2D
#
# # Assuming `dqn_bool_action_df` and `bool_action_bm_df` are your DataFrames
# dqn_df = dqn_bool_action_df.copy()
# bm_df = bool_action_bm_df.copy()
#
# # Function to find transition points
# def find_transitions(series):
# transitions = np.where(series.diff() != 0)
# return transitions
#
# # Find transitions for both DataFrames
# dqn_transitions = find_transitions(dqn_df.iloc[:, 0])
# bm_transitions = find_transitions(bm_df.iloc[:, 0])
#
# # Plotting
# fig, ax = plt.subplots(figsize=(12, 6))
#
# # Plotting the binary outputs
# sns.heatmap(dqn_df.T, cmap='cividis', ax=ax, cbar=False, linewidths=0.5, linecolor='black', alpha=0.5)
# sns.heatmap(bm_df.T, cmap='cividis', ax=ax, cbar=False, linewidths=0.5, linecolor='black', alpha=0.5)
#
# # Plotting vertical lines for transitions
# for transition in dqn_transitions:
# ax.axvline(x=transition, color='red', linestyle='--', linewidth=1)
#
# for transition in bm_transitions:
# ax.axvline(x=transition, color='blue', linestyle='--', linewidth=1)
#
# # Annotate data point values
# for i in range(len(dqn_df.columns)):
# for j in range(len(dqn_df)):
# ax.text(i + 0.5, j + 0.5, int(dqn_df.iloc[j, i]), ha='center', va='center', color='white', fontsize=8)
#
# # Create proxy artists for legend
# legend_elements = [
# Line2D([0], [0], color='red', linestyle='--', label='DQN Transition'),
# Line2D([0], [0], color='blue', linestyle='--', label='DP Transition'),
# Patch(facecolor='white', edgecolor='black', label='DQN Binary Output'),
# ]
#
# plt.axvline(x=optimal_weight[0]*100, color='red', linestyle='--', label='Optimal Weight')
#
#
# # Set labels and title
# ax.set_xlabel("Observations")
# ax.set_ylabel("Transaction Costs")
# ax.set_title("Comparison of Binary Outputs with Transitions")
#
# # Add legend with custom elements
# ax.legend(handles=legend_elements)
#
# plt.show()
In [30]:
# import plotly.graph_objs as go
#
# # Assuming `dqn_bool_action_df` and `bool_action_bm_df` are your DataFrames
# dqn_df = dqn_bool_action_df.copy()
# bm_df = bool_action_bm_df.copy()
#
# # Get the column labels for x-axis
# column_labels_dqn = dqn_df.columns.astype(str).tolist()
# column_labels_bm = bm_df.columns.astype(str).tolist()
#
# # Create boxplots for DQN Binary
# box_dqn = go.Box(y=dqn_df.stack().values, x=column_labels_dqn * len(dqn_df.index), name="DQN Binary", boxpoints='all', jitter=0.3, pointpos=-1.8)
#
# # Create boxplots for DP Bellman
# box_dp = go.Box(y=bm_df.stack().values, x=column_labels_bm * len(bm_df.index), name="DP Bellman", boxpoints='all', jitter=0.3, pointpos=-1.8)
#
# # Create the layout with 'overlay' parameter set to "overlay"
# layout = go.Layout(
# title="Comparison of DQN Binary and DP Bellman",
# xaxis=dict(title="Transaction Cost (bps)"),
# yaxis=dict(title="Weight on First Asset"),
# barmode="overlay" # Set the barmode to overlay
# )
#
# # Create the figure
# fig = go.Figure(data=[box_dqn, box_dp], layout=layout)
#
# # Show the figure
# fig.show()
Insert Tuple to Check¶
4 Assets¶
In [1]:
# n_asset = 4
# tc = 0.002
# mu = np.linspace(50, 200, n_asset) / 1e4
# sigma = np.linspace(300, 800, n_asset) / 1e4
# cov = np.diag(sigma ** 2)
# agent = Agent(mu=mu, cov=cov, batch_size=BATCH_SIZE, gamma=GAMMA,
# eps_start=EPS_START, eps_end=EPS_END, eps_decay=EPS_DECAY, tau=TAU, lr=LR,
# tc=tc)
In [2]:
# state = torch.Tensor(agent.w_optimal).unsqueeze(0)
# state
In [33]:
# state = torch.Tensor([100, -100, 1, 0]).unsqueeze(0)
# state
In [3]:
# a = agent.policy_net(state).max(1)[1].view(1, 1)
# print(agent.policy_net(state))
# if a.item() == 1:
# print(f"Action = {a.item()} -> You should rebalance!")
# else:
# print(f"Action = {a.item()} -> You should NOT rebalance!")
In [4]:
# agent.policy_net(state).argmax()
In [5]:
# def get_high_dim_tensors(og_tensor, num_steps=10):
# # Calculate the increase and decrease values
# increase_values = torch.linspace(0, 1 - og_tensor[0, 0], num_steps)
# decrease_value = increase_values / (og_tensor.size(1) - 1)
# # Create a list of tensors
# result_list = []
# a = torch.zeros(og_tensor.size(1))
# a[0] = 1
# b = torch.full((og_tensor.size(1),), 1 / (og_tensor.size(1) - 1))
# b[0] = 0
# c = torch.full((og_tensor.size(1),), 1 / (og_tensor.size(1)))
# result_list.append(a)
# result_list.append(b)
# result_list.append(c)
# for i in range(num_steps):
# updated_tensor = og_tensor.clone()
# updated_tensor[0, 0] += increase_values[i]
# updated_tensor[0, 1:] -= decrease_value[i]
# if torch.any(torch.lt(updated_tensor, 0)):
# break
# result_list.append(updated_tensor)
# return result_list
In [6]:
# # og_tensor = torch.tensor([[0.1, 0.3, 0.3, 0.3]])
# high_dim_tensors = get_high_dim_tensors(og_tensor=state, num_steps=10)
# high_dim_tensors[:5]
In [7]:
# out = []
# for i in high_dim_tensors:
# a = agent.policy_net(state).max(1)[1].view(1, 1)
# out.append(a.item())
# if a.item() == 1:
# print(f"Action = {a.item()} -> You should rebalance!")
# else:
# print(f"Action = {a.item()} -> You should NOT rebalance!")
8 Assets¶
In [8]:
# n_asset = 8
# tc = 0.002
# mu = np.linspace(50, 200, n_asset) / 1e4
# sigma = np.linspace(300, 800, n_asset) / 1e4
# cov = np.diag(sigma ** 2)
# agent = Agent(mu=mu, cov=cov, batch_size=BATCH_SIZE, gamma=GAMMA,
# eps_start=EPS_START, eps_end=EPS_END, eps_decay=EPS_DECAY, tau=TAU, lr=LR,
# tc=tc)
In [9]:
# state = torch.Tensor(agent.w_optimal).unsqueeze(0)
# state
In [10]:
# a = agent.policy_net(state).max(1)[1].view(1, 1)
# print(agent.policy_net(state))
# if a.item() == 1:
# print(f"Action = {a.item()} -> You should rebalance!")
# else:
# print(f"Action = {a.item()} -> You should NOT rebalance!")
In [11]:
# # og_tensor = torch.tensor([[0.1, 0.3, 0.3, 0.3]])
# high_dim_tensors = get_high_dim_tensors(og_tensor=state, num_steps=10)
# high_dim_tensors[:5]
In [12]:
# out = []
# for i in high_dim_tensors:
# a = agent.policy_net(state).max(1)[1].view(1, 1)
# out.append(a.item())
# if a.item() == 1:
# print(f"Action = {a.item()} -> You should rebalance!")
# else:
# print(f"Action = {a.item()} -> You should NOT rebalance!")
32 Assets¶
In [13]:
# n_asset = 32
# tc = 0.002
# mu = np.linspace(50, 200, n_asset) / 1e4
# sigma = np.linspace(300, 800, n_asset) / 1e4
# cov = np.diag(sigma ** 2)
# agent = Agent(mu=mu, cov=cov, batch_size=BATCH_SIZE, gamma=GAMMA,
# eps_start=EPS_START, eps_end=EPS_END, eps_decay=EPS_DECAY, tau=TAU, lr=LR,
# tc=tc)
In [14]:
# state = torch.Tensor(agent.w_optimal).unsqueeze(0)
# state
In [15]:
# a = agent.policy_net(state).max(1)[1].view(1, 1)
# print(agent.policy_net(state))
# if a.item() == 1:
# print(f"Action = {a.item()} -> You should rebalance!")
# else:
# print(f"Action = {a.item()} -> You should NOT rebalance!")
In [16]:
# # og_tensor = torch.tensor([[0.1, 0.3, 0.3, 0.3]])
# high_dim_tensors = get_high_dim_tensors(og_tensor=state, num_steps=10)
# high_dim_tensors[:5]
In [17]:
# out = []
# for i in high_dim_tensors:
# a = agent.policy_net(state).max(1)[1].view(1, 1)
# out.append(a.item())
# if a.item() == 1:
# print(f"Action = {a.item()} -> You should rebalance!")
# else:
# print(f"Action = {a.item()} -> You should NOT rebalance!")
128 Assets¶
In [18]:
# n_asset = 128
# tc = 0.002
# mu = np.linspace(50, 200, n_asset) / 1e4
# sigma = np.linspace(300, 800, n_asset) / 1e4
# cov = np.diag(sigma ** 2)
# agent = Agent(mu=mu, cov=cov, batch_size=BATCH_SIZE, gamma=GAMMA,
# eps_start=EPS_START, eps_end=EPS_END, eps_decay=EPS_DECAY, tau=TAU, lr=LR,
# tc=tc)
Bar Chart to visualize the optimal¶
In [19]:
# state = torch.Tensor(agent.w_optimal).unsqueeze(0)
# state
In [20]:
# a = agent.policy_net(state).max(1)[1].view(1, 1)
# print(agent.policy_net(state))
# if a.item() == 1:
# print(f"Action = {a.item()} -> You should rebalance!")
# else:
# print(f"Action = {a.item()} -> You should NOT rebalance!")
In [21]:
# # og_tensor = torch.tensor([[0.1, 0.3, 0.3, 0.3]])
# high_dim_tensors = get_high_dim_tensors(og_tensor=state, num_steps=10)
# high_dim_tensors[:5]
In [22]:
# out = []
# for i in high_dim_tensors:
# a = agent.policy_net(state).max(1)[1].view(1, 1)
# out.append(a.item())
# if a.item() == 1:
# print(f"Action = {a.item()} -> You should rebalance!")
# else:
# print(f"Action = {a.item()} -> You should NOT rebalance!")
In [ ]:
In [ ]: